package com.tom.utils;

import cn.hutool.core.io.IoUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSONReader;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Map;

/**
 * 读取Json文件转换成想要的对象
 * @author zx
 * @date 2024年07月10日 18:13
 */
public class ResponJsonDataUtils {

    public static List<Map<String, Object>> loadJsonToListMap(String filePath) {//转换成Map
        try {
            // 加载资源文件
            Resource resource = new ClassPathResource(filePath);
            // 创建 ObjectMapper 实例
            ObjectMapper objectMapper = new ObjectMapper();
            // 将 JSON 文件转换为 List<Map<String, Object>> 类型
            return objectMapper.readValue(resource.getInputStream(), objectMapper.getTypeFactory().constructCollectionType(List.class, Map.class));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static <T> List<T> loadJsonToListMap(String filePath, Class<T> clazz) {//转换成对应的类型结合对象
        try {
            // 加载资源文件
            Resource resource = new ClassPathResource(filePath);
            // 创建 ObjectMapper 实例
            ObjectMapper objectMapper = new ObjectMapper();
            // 将 JSON 文件转换为 List<Map<String, Object>> 类型
            return objectMapper.readValue(resource.getInputStream(), objectMapper.getTypeFactory().constructCollectionType(List.class, clazz));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


    public static JSONObject loadJsonToObject(String filePath) {

        // 获取资源文件的输入流
        InputStream inputStream = ResponJsonDataUtils.class.getResourceAsStream(filePath);
        if (inputStream == null) {
            throw new RuntimeException("Resource not found.");
        }
        // 使用IOUtils读取整个流到字符串
        String jsonString = IoUtil.toStr(convert(inputStream), StandardCharsets.UTF_8);
        //String jsonString = IOUtils.toString(inputStream, StandardCharsets.UTF_8);
        return JSONUtil.parseObj(jsonString);
    }

    private static ByteArrayOutputStream convert(InputStream inputStream) {
        ByteArrayOutputStream byteArrayOutputStream = null;
        try {
            byteArrayOutputStream = new ByteArrayOutputStream();
            byte[] buffer = new byte[1024];
            int length;

            while ((length = inputStream.read(buffer)) != -1) {
                byteArrayOutputStream.write(buffer, 0, length);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        return byteArrayOutputStream;
    }


    public static <T> T jsonFileToObject(String file, Class<T> clazz) {//转换成对应的对象
        // 创建 ObjectMapper 实例
        ObjectMapper mapper = new ObjectMapper();
        T obj = null;
        try {

            obj = mapper.readValue(JSONReader.class.getResourceAsStream(file), clazz);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return obj;
    }


    public static <T> List<T> jsonFileToCollectionObject(String file, T t) {

        List<T> list = null;
        try {
            ObjectMapper mapper = new ObjectMapper();

            // 使用 ClassPathResource 来访问 resources 目录下的文件
            ClassPathResource resource = new ClassPathResource(file);

            // 获取输入流
            InputStream inputStream = resource.getInputStream();

            // 使用 Jackson 将 JSON 文件转换为 List 对象
            list = mapper.readValue(inputStream, new TypeReference<List<T>>() {
            });

            // 打印集合中的对象
            list.forEach(System.out::println);

        } catch (Exception e) {
            e.printStackTrace();
        }

        return list;

    }


}
